package com.yangxk.kadmin.common.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;

public class NumberUtil {
    private NumberUtil(){}

    /**
     * 格式化值
     * @param value 需要格式化的值
     * @param scale 需要保留的小数点位数
     * @param roundingMode 舍入模式
     * create by yangxk on 2018/9/12 15:55
     */
    public static String format(double value, int scale, int roundingMode) {
        BigDecimal val = new BigDecimal(value);
        return val.setScale(scale, roundingMode).toString();
    }

    /**
     * 四舍五入保留1位小数
     * create by yangxk on 2018/9/12 15:59
     */
    public static String format1Point(double value) {
        return format(value, 1, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 四舍五入保留2位小数
     * create by yangxk on 2018/9/12 15:59
     */
    public static String format2Point(double value) {
        return format(value, 2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 百分比
     * @param value 需要计算百分比的值
     * @param scale 保留的小数位数
     * @param roundingMode 舍入模式
     * create by yangxk on 2018/9/12 16:28
     */
    public static String percent(double value, int scale, RoundingMode roundingMode) {
        // 第一种实现方式
        NumberFormat percent = NumberFormat.getPercentInstance();
        percent.setGroupingUsed(false);
        percent.setMaximumFractionDigits(scale);
        percent.setRoundingMode(roundingMode);
        return percent.format(value);

        // 第二种实现方式
//        StringBuilder sb = new StringBuilder();
//        sb.append("#.");
//        for (int i = 0; i < scale; i++) {
//            sb.append("#");
//        }
//        sb.append("%");
//        DecimalFormat format = new DecimalFormat(sb.toString());
//        format.setRoundingMode(RoundingMode.valueOf(roundingMode));
//        return format.format(value);
    }


    /**
     * 百分比 四舍五入保留1位小数
     * create by yangxk on 2018/9/12 16:31
     */
    public static String percent1Point(double value) {
        return percent(value, 1, RoundingMode.HALF_UP);
    }

    /**
     * 百分比 四舍五入保留2位小数
     * create by yangxk on 2018/9/12 16:31
     */
    public static String percent2Point(double value) {
        return percent(value, 2, RoundingMode.HALF_UP);
    }

    /**
     * decimal计算类
     * @author yangxk
     * @date 2018/9/12 16:33
     */
    public static class DecimalCalc {
        private BigDecimal val;
        private DecimalCalc(BigDecimal val) {
            this.val = val;
        }

        public static DecimalCalc of(double val) {
            return new DecimalCalc(new BigDecimal(val));
        }

        /**
         * 加
         * create by yangxk on 2018/9/12 16:41
         */
        public DecimalCalc add(double value) {
            this.val.add(new BigDecimal(value));
            return this;
        }

        /**
         * 减
         * create by yangxk on 2018/9/12 16:41
         */
        public DecimalCalc sub(double value) {
            this.val.subtract(new BigDecimal(value));
            return this;
        }

        /**
         * 乘
         * create by yangxk on 2018/9/12 16:41
         */
        public DecimalCalc mul(double value) {
            this.val.multiply(new BigDecimal(value));
            return this;
        }

        /**
         * 除
         * create by yangxk on 2018/9/12 16:41
         */
        public DecimalCalc div(double value, int scale, int roundingMode) {
            this.val.divide(new BigDecimal(value), scale, roundingMode);
            return this;
        }

        /**
         * 除
         * 四舍五入保留2位小数
         * create by yangxk on 2018/9/12 16:41
         */
        public DecimalCalc div(double value) {
            div(value, 2, BigDecimal.ROUND_HALF_UP);
            return this;
        }

        /**
         * 除
         * 四舍五入
         * create by yangxk on 2018/9/12 16:47
         */
        public DecimalCalc div(double value, int scale) {
            div(value, scale, BigDecimal.ROUND_HALF_UP);
            return this;
        }

        /**
         * 获取精确值
         * create by yangxk on 2018/9/12 16:45
         */
        public double getValue() {
            return this.val.doubleValue();
        }

        /**
         * 获取指定模式下的值
         * create by yangxk on 2018/9/12 16:46
         */
        public double getValue(int scale, int roundingMode) {
            return this.val.setScale(scale, roundingMode).doubleValue();
        }

        /**
         * 获取百分比
         * create by yangxk on 2018/9/12 16:49
         */
        public String getPercent(int scale, int roundingMode) {
            return NumberUtil.percent(val.doubleValue(), scale, RoundingMode.valueOf(roundingMode));
        }

        /**
         * 获取百分比
         * 四舍五入保留2位小数
         * create by yangxk on 2018/9/12 16:59
         */
        public String getPercent() {
            return getPercent(2, BigDecimal.ROUND_HALF_UP);
        }
    }

    public static void main(String[] args) {

    }
}
